package com.yueyang.sojourn.server.controller.wx;

import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.common.baymax.web.result.JAssert;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.model.dto.AddTouristDto;
import com.yueyang.sojourn.server.model.dto.EditTouristDto;
import com.yueyang.sojourn.server.model.enums.StatusEnum;
import com.yueyang.sojourn.server.model.po.SojournTourist;
import com.yueyang.sojourn.server.model.vo.SojournTouristVo;
import com.yueyang.sojourn.server.service.sojourn.tourist.SojournTouristService;
import com.yueyang.uc.model.UcUserV1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.HttpStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author andy.yang
 * @version v1
 * @summary
 * @Copyright (c) 2019, yueyang Group All Rights Reserved.
 * @since 2020/3/17
 */
@RestController
@RequestMapping("/api/wx/tourist")
@Api(tags = "(小程序)游客 API")
public class ApiTouristController extends BaseController {

    @Autowired
    SojournTouristService touristService;

    /**
     * 新增游客
     * @param touristDto
     * @return
     */
    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "新增游客")
    @PostMapping("")
    public Long createTourist(@RequestBody @Validated AddTouristDto touristDto, BindingResult bindingResult){
        if (bindingResult.getAllErrors() != null && bindingResult.getAllErrors().size() > 0) {
            JAssert.justFailed(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }

        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();

        SojournTourist tourist = new SojournTourist();
        BeanUtils.copyProperties(touristDto,tourist);
        tourist.setCreator(loginUserId);
        Long touristId = touristService.add(tourist);
        return touristId;
    }


    /**
     * 编辑游客
     * @param touristDto
     * @return
     */
    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "编辑游客")
    @PutMapping("")
    public void updateTourist(@RequestBody @Validated EditTouristDto touristDto,BindingResult bindingResult,HttpServletResponse response){
        if (bindingResult.getAllErrors() != null && bindingResult.getAllErrors().size() > 0) {
            JAssert.justFailed(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }


        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();

        SojournTourist tourist = touristService.findById(touristDto.getId());
        if(tourist == null || StatusEnum.INVALID.getCode() == tourist.getStatus()){
            return;
        }
        BeanUtils.copyProperties(touristDto,tourist);
        tourist.setUpdator(loginUserId);
        touristService.update(tourist);
    }

    /**
     * 删除游客
     * @param touristId
     * @return
     */
    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "删除游客")
    @DeleteMapping("/{touristId}")
    public void delOrder(@ApiParam(value = "订单id") @PathVariable Long touristId, HttpServletResponse response){
        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();
        SojournTourist tourist = touristService.findById(touristId);
        if(tourist == null || !tourist.getCreator().equals(loginUserId)){
//            response.setStatus(HttpStatus.SC_NOT_FOUND);
            return;
        }
        touristService.delete(touristId,loginUserId);
    }

    /**
     * 我的游客
     * @param
     * @return
     */
    @ApiOperation(value = "游客列表")
    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @GetMapping("/mine")
    public PageInfo<SojournTouristVo> tourists(@ApiParam(value = "页码",defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNo,
                                               @ApiParam(value = "一页数量",defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize){
        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();
        PageInfo<SojournTourist> pageInfo = touristService.findPage(loginUserId, StatusEnum.VALID.getCode(),pageNo,pageSize);

        PageInfo<SojournTouristVo> page = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo,page);
        page.setList(pageInfo.getList().parallelStream()
                .map(po -> {
                    SojournTouristVo vo = new SojournTouristVo();
                    BeanUtils.copyProperties(po,vo);
                    return vo;})
                .collect(Collectors.toList()));
        return page;
    }

    /**
     * 游客详情
     * @param touristId
     * @return
     */
    @ApiOperation(value = "游客详情")
    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @GetMapping("/{touristId}")
    public SojournTouristVo orderDetail(@ApiParam(value = "游客id") @PathVariable Long touristId,HttpServletResponse response){
        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();
        SojournTourist tourist = touristService.findById(touristId);
        if(tourist == null || !tourist.getCreator().equals(loginUserId)){
//            response.setStatus(HttpStatus.SC_NOT_FOUND);
            return null;
        }
        SojournTouristVo touristVo = new SojournTouristVo();
        BeanUtils.copyProperties(tourist,touristVo);
        return touristVo ;
    }
}
